<?php

/**
 * 此示例为 微信支付（H5 invoke 方式）
 * https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7
 */

require './config.php';

use Thenbsp\Wechat\Util\Util;
use Thenbsp\Wechat\OAuth;
use Thenbsp\Wechat\Payment;
use Thenbsp\Wechat\Payment\Order;
use Thenbsp\Wechat\Payment\Business;
use Thenbsp\Wechat\Payment\Unifiedorder;
use Thenbsp\Wechat\Exception\PaymentException;

/**
 * 以 JSAPI 方式的付款需要获取用户 Openid
 */
if(false &&  !isset($_SESSION['openid']) ) {
    $o = new OAuth(APPID, APPSECRET);
    if( !isset($_GET['code']) ) {
        $o->authorize(Util::currentUrl());
    } else {
        $token = $o->getAccessToken($_GET['code']);
        $_SESSION['openid'] = $token->openid;
    }
}

/**
 * 第 1 步：定义商户
 * 设置商户证书路径请使用 setClientCert/setClientKey
 * $business->setClientCert('/your/path/to/apiclient_cert.pem');
 * $business->setClientKey('/your/path/to/apiclient_key.pem');
 */
$business = new Business();
$business->appid(APPID);
$business->appsecret(APPSECRET);
$business->mch_id(MCHID);
$business->mch_key(MCHKEY);
$business->setClientCert('/data/apache_srv/qatest/system/config/cert/apiclient_cert.pem');
$business->setClientKey('/data/apache_srv/qatest/system/config/cert/apiclient_key.pem');

// print_r($business->getParams());

/**
 * 第 2 步：定义订单
 */
$order = new Order();
$order->body('iphone 6 plus');
$order->out_trade_no(date('Y-m-dHis').mt_rand(10000, 99999));
$order->total_fee('1');
// $order->openid($_SESSION['openid']);
$order->openid('o6XLUsvPEQ9dX5DOLMSMGcqN_AHc');
$order->notify_url('http://nutri.qiezilife.com/example/payment_notify.php');

// print_r($order->getParams());

/**
 * 第 3 步：统一下单
 */
$unifiedorder = new Unifiedorder();
$unifiedorder->setBusiness($business);
$unifiedorder->setOrder($order);

/**
 * 第 4 步：生成支付配置文件
 */
$o = new Payment($unifiedorder);
$configJSON = $o->getConfig();

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Wechat SDK</title>
</head>
<body ontouchstart="">

<h1>微信支付测试&nbsp;&nbsp;<a href="javascript:;" onclick="window.location.reload()">刷新</a></h1>
<h4>Github: <a href="http://github.com/thenbsp/Wechat">http://github.com/thenbsp/Wechat</a></h4>
<h4>WeixinJSBridge invoke 方式：</h4>
<button type="button" onclick="WXPayment()" style="font-size:16px;height:38px;">支付 ￥<?php echo ($order->getParams('total_fee') / 100); ?> 元</button>

<script>
var WXPayment = function() {

    if( typeof WeixinJSBridge === 'undefined' ) {
        alert('请在微信在打开页面！');
        return false;
    }

    WeixinJSBridge.invoke('getBrandWCPayRequest', <?php echo $configJSON; ?>, function(res) {
        switch(res.err_msg) {
            case 'get_brand_wcpay_request:cancel':
                alert('用户取消支付！');
                break;
            case 'get_brand_wcpay_request:fail':
                alert('支付失败！（'+res.err_desc+'）');
                break;
            case 'get_brand_wcpay_request:ok':
                alert('支付成功！');
                break;
            default:
                alert(JSON.stringify(res));
                break;
        }
    });
}
</script>
</body>
</html>